package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.weixing.mall.base.enums.SnCodeEnum;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.model.vo.dto.BillSaveDto;
import com.weixing.mall.provider.model.domain.GoodsTransBill;
import com.weixing.mall.provider.model.domain.GoodsTransBillProduct;
import com.weixing.mall.provider.model.dto.ChgTransStatusDto;
import com.weixing.mall.provider.model.dto.GoodsTransBillDto;
import com.weixing.mall.provider.model.dto.NewGoodsTransBillDto;
import com.weixing.mall.provider.model.enums.TransStatusEunm;
import com.weixing.mall.provider.model.query.GoodsTransBillQuery;
import com.weixing.mall.provider.model.query.GoodsTransQuery;
import com.weixing.mall.provider.model.vo.GoodsTransBillListVo;
import com.weixing.mall.provider.model.vo.GoodsTransBillProductVo;
import com.weixing.mall.provider.service.IGoodsTransBillOperationService;
import com.weixing.mall.provider.service.IGoodsTransBillProductService;
import com.weixing.mall.provider.service.IGoodsTransBillService;
import com.weixing.mall.provider.support.excalservice.DepotExcalService;
import com.weixing.mall.provider.util.DepotUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName AdminGoodsTransBillController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/10/28
 * @Version 1.0
 **/
@RequestMapping("goodsTransBill")
@RestController
@Api(value = "调货单Api",tags = "调货单")
public class GoodsTransBillController extends BaseController {

    @Autowired
    private IGoodsTransBillService goodsTransBillService;
    @Autowired
    private IGoodsTransBillProductService goodsTransBillProductService;
    @Autowired
    private IGoodsTransBillOperationService goodsTransBillOperationService;


    @ApiOperation(value = "查询调拨单列表")
    @GetMapping("")
    public Result selectList(GoodsTransBillQuery goodsTransBillQuery,@SysInfo SysAuth auth){
        return ResultUtil.success(goodsTransBillService.selectListOfPage(initMpPage(),goodsTransBillQuery,auth.getUid()));
    }

    @ApiOperation(value = "根据id查找调拨单")
    @GetMapping("getGoodsTransBillById/{id}")
    public Result getGoodsTransBillById(@PathVariable Long id){
        return ResultUtil.success(goodsTransBillService.getGoodsTransBillById(id));
    }
    @ApiOperation(value = "调货出库-出库单详情导出Excal")
    @GetMapping("excel/{id}")
    @SysLog(type = OperateEnum.E,desp = "调货出库-出库单详情导出")
    @NoNeedAccess
    public void excel(@PathVariable Long id, HttpServletResponse response){
        GoodsTransBillListVo gtblv = goodsTransBillService.getGoodsTransBillById(id);
        List<GoodsTransBillProductVo> list =goodsTransBillProductService.getGoodsTransBillProductByGoodsTransId(id,null,new GoodsTransQuery());
        DepotExcalService.GoodsTransBillProductExcalDownload(gtblv.getTransBillSn(),gtblv.getOutputDepotName(),gtblv.getInputDepotName(),list,"调货出库单导出",response);
    }

    @ApiOperation(value = "初始化新增")
    @GetMapping("initAdd")
    public Result initAdd(){
        return ResultUtil.success(goodsTransBillService.creatTransBill(SnCodeEnum.TRANSBILLSN.getValue()));
    }
    @ApiOperation(value = "调拨单申请/保存调拨单及关联商品信息")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@SysInfo SysAuth auth, @RequestBody GoodsTransBillDto goodsTransBillDto){
       /* boolean checkTransBill=goodsTransBillService.checkTransBill(goodsTransBillDto);
        if(checkTransBill){
            return ResultUtil.error("调拨存在出库大于库存订单，不允许出库");
        }*/
        if(ObjectUtil.isNull(goodsTransBillDto.getFhName())){
            goodsTransBillDto.setRemark(goodsTransBillDto.getFhName());
        }
        long goodsTransBillId =  goodsTransBillService.create(goodsTransBillDto,auth.getUid());
        return ResultUtil.success(goodsTransBillId);
    }

    @ApiOperation(value = "修改调拨单备注信息")
    @PostMapping("updateRemark")
    @NoRepeatSubmit
    public Result updateRemark(@SysInfo SysAuth auth, @RequestBody NewGoodsTransBillDto newGoodsTransBillDto){
        Boolean flag =  goodsTransBillService.updateRemark(newGoodsTransBillDto,auth.getUid());
        return flag?ResultUtil.success("成功"):ResultUtil.error("失败");
    }

    @ApiOperation(value = "修改调拨单关联商品信息")
    @PostMapping("updateGoodsInfo")
    @NoRepeatSubmit
    public Result updateGoodsInfo(@SysInfo SysAuth auth, @RequestBody NewGoodsTransBillDto newGoodsTransBillDto){
        boolean checkTransBill=goodsTransBillService.checkTransBillProduct(newGoodsTransBillDto);
        if(checkTransBill){
            return ResultUtil.error("调拨存在出库大于库存订单，不允许出库");
        }
        boolean flag =  goodsTransBillService.updateGoodsInfo(newGoodsTransBillDto,auth.getUid());
        return flag?ResultUtil.success("成功"):ResultUtil.error("失败");
    }


  @ApiOperation(value = "修改调拨单状态")
  @PostMapping("chgTransStatus")
  @NoRepeatSubmit
    public Result chgTransStatus(@SysInfo SysAuth auth,ChgTransStatusDto chgTransStatusDto){
        goodsTransBillService.chgTransStatus(chgTransStatusDto,auth.getUid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "订单模块调拨单生成")
    @GetMapping("transBillGenerate")
    public Result transBillGenerate(@SysInfo SysAuth auth){
        goodsTransBillService.tranBillGenerate(auth.getUid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除调拨单")
    @GetMapping("del/{id}")
    public Result del(@PathVariable Long id){
        GoodsTransBill bill=goodsTransBillService.getById(id);
        if(!bill.getTransStatus().equals(TransStatusEunm.TYPE_1.getValue())){
            return  ResultUtil.error("已审核的调拨单不允许删除");
        }
        goodsTransBillService.removeById(id);
        goodsTransBillProductService.remove(new QueryWrapper<GoodsTransBillProduct>().eq("goods_trans_id",id));
        return ResultUtil.success();
    }
    @ApiOperation(value = "调拨单申请/保存调拨单及关联商品信息")
    @PostMapping("test")
    @NoRepeatSubmit
    public Result test(BillSaveDto billSaveDto){
        goodsTransBillService.createBill(billSaveDto);

        return ResultUtil.success();
    }

    @ApiOperation(value = "货单作废操作")
    @GetMapping("updateBillStatus/{id}")
    @NoRepeatSubmit
    public Result updateBillStatus(@PathVariable(value = "id")Long id){
        goodsTransBillService.update(new UpdateWrapper<GoodsTransBill>().eq("id",id).set("bill_status",-1));
        return ResultUtil.success();
    }
}
